## Load conjoint attributes from source files

sub_rep_names <- c("sub_rep_1_1", "sub_rep_1_2", "sub_rep_1_3", "sub_rep_1_4")
surr_rep_names <- c("surr_rep_1_1", "surr_rep_1_2", "surr_rep_1_3")
desc_rep_names <- c("desc_rep_1_1", "desc_rep_1_2", "desc_rep_1_3", "desc_rep_1_4", "desc_rep_1_5")
just_rep_names <- c("just_rep1_1", "just_rep1_2", "just_rep1_3", "just_rep1_4")
pers_rep_names <- c("pers_rep1_1", "pers_rep1_2", "pers_rep1_3", "pers_rep1_4")
resp_rep_names <- c("respons_rep1_1", "respons_rep1_2", "respons_rep1_3")

all_names <- c(sub_rep_names, surr_rep_names, desc_rep_names, just_rep_names, pers_rep_names, resp_rep_names)

est_factors <- function(n_boots = 10, attn){
  
  ## Load survey data
  
  load(paste0("../working/survey_data",attn,".Rdata"))
  
  ## ###########
  ## Factor analysis of dimensions
  # wgt <- x$wgt
  # x <- x[,-4]
  
  fa_func <- function(x,wgt) {
    
    for(i in 1:ncol(x)){ # Hacky fixed to add one example of each outcome category to each variable
      # Only relevant for very rare instances where bootstrap dataset excludes one category entirely
      
      if(length(na.omit(unique(x[[i]]))) != 5){
        to_add <- c(1:5)[!c(1:5) %in% x[[i]]]
        x[[i]][sample(1:length(x[[i]]),1)] <- to_add
      }
      
    }
    
    out <- as.numeric(fa(x, nfactors = 1, fm = "ml", max.iter = 500, cor = "poly", warnings = FALSE, weight = wgt)$scores)
    
    return(out)
  }
  
  
  fa_uk <- function(uk){
    
    uk <- analysis(uk) %>% 
      mutate(across(all_of(all_names), ~as.numeric(factor(.x,levels = c("Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree"))), .names = "{.col}_num")) %>%
      # Apply factor analysis function
      mutate(sub_fa = fa_func(select(.,paste0(sub_rep_names,"_num")), wgt = wgt),
             surr_fa = fa_func(select(.,paste0(surr_rep_names,"_num")), wgt = wgt),
             desc_fa = fa_func(select(.,paste0(desc_rep_names,"_num")), wgt = wgt),
             just_fa = fa_func(select(.,paste0(just_rep_names,"_num")), wgt = wgt),
             pers_fa = fa_func(select(.,paste0(pers_rep_names,"_num")), wgt = wgt),
             resp_fa = fa_func(select(.,paste0(resp_rep_names,"_num")), wgt = wgt)
      ) 
    
    if(cor(uk$sub_fa, uk$sub_rep_1_1_num, use = "complete.obs") < 0) uk$sub_fa <- uk$sub_fa*-1 # Higher means wants more substantive representation
    if(cor(uk$surr_fa, uk$surr_rep_1_1_num, use = "complete.obs") > 0) uk$surr_fa <- uk$surr_fa*-1 # Higher means wants (or will tolerate) more surrogate representation
    if(cor(uk$desc_fa, uk$desc_rep_1_1_num, use = "complete.obs") < 0) uk$desc_fa <- uk$desc_fa*-1 # Higher means wants more descriptive representation
    if(cor(uk$just_fa, uk$just_rep1_1_num, use = "complete.obs") < 0) uk$just_fa <- uk$just_fa*-1 # Higher means wants politicians to appeal to society as a whole
    if(cor(uk$pers_fa, uk$pers_rep1_4_num, use = "complete.obs") < 0) uk$pers_fa <- uk$pers_fa*-1 # Higher means wants more personalization
    if(cor(uk$resp_fa, uk$respons_rep1_2_num, use = "complete.obs") < 0) uk$resp_fa <- uk$resp_fa*-1 # Higher means wants more responsiveness
    
    uk
    
  }
  
  
  fa_us <- function(us){
    
    us <- analysis(us) %>% 
      # Recode batteries to numeric
      mutate(across(all_of(all_names), ~as.numeric(factor(.x,levels = c("Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree"))), .names = "{.col}_num")) %>%
      # Apply factor analysis function
      mutate(sub_fa = fa_func(select(.,paste0(sub_rep_names,"_num")), wgt = wgt),
             surr_fa = fa_func(select(.,paste0(surr_rep_names,"_num")), wgt = wgt),
             desc_fa = fa_func(select(.,paste0(desc_rep_names,"_num")), wgt = wgt),
             just_fa = fa_func(select(.,paste0(just_rep_names,"_num")), wgt = wgt),
             pers_fa = fa_func(select(.,paste0(pers_rep_names,"_num")), wgt = wgt),
             resp_fa = fa_func(select(.,paste0(resp_rep_names,"_num")), wgt = wgt)
      )
    
    if(cor(us$sub_fa, us$sub_rep_1_1_num, use = "complete.obs") < 0) us$sub_fa <- us$sub_fa*-1 # Higher means wants more substantive representation
    if(cor(us$surr_fa, us$surr_rep_1_1_num, use = "complete.obs") > 0) us$surr_fa <- us$surr_fa*-1 # Higher means wants (or will tolerate) more surrogate representation
    if(cor(us$desc_fa, us$desc_rep_1_1_num, use = "complete.obs") < 0) us$desc_fa <- us$desc_fa*-1 # Higher means wants more descriptive representation
    if(cor(us$just_fa, us$just_rep1_1_num, use = "complete.obs") < 0) us$just_fa <- us$just_fa*-1 # Higher means wants politicians to appeal to society as a whole
    if(cor(us$pers_fa, us$pers_rep1_4_num, use = "complete.obs") < 0) us$pers_fa <- us$pers_fa*-1 # Higher means wants more personalization
    if(cor(us$resp_fa, us$respons_rep1_2_num, use = "complete.obs") < 0) us$resp_fa <- us$resp_fa*-1 # Higher means wants more responsiveness
    
    us
    
  }
  
  
  fa_de <- function(de){
    
    de <- analysis(de) %>% 
      # Recode batteries to numeric
      mutate(across(all_of(all_names), ~as.numeric(factor(.x,levels = c("Stimme überhaupt nicht zu", "Stimme nicht zu", "Stimme weder zu noch nicht zu", "Stimme zu", "Stimme voll und ganz zu"))), .names = "{.col}_num")) %>%
      # Apply factor analysis function
      mutate(sub_fa = fa_func(select(.,paste0(sub_rep_names,"_num")), wgt = wgt),
             surr_fa = fa_func(select(.,paste0(surr_rep_names,"_num")), wgt = wgt),
             desc_fa = fa_func(select(.,paste0(desc_rep_names,"_num")), wgt = wgt),
             just_fa = fa_func(select(.,paste0(just_rep_names,"_num")), wgt = wgt),
             pers_fa = fa_func(select(.,paste0(pers_rep_names,"_num")), wgt = wgt),
             resp_fa = fa_func(select(.,paste0(resp_rep_names,"_num")), wgt = wgt)
      ) 
    
    if(cor(de$sub_fa, de$sub_rep_1_1_num, use = "complete.obs") < 0) de$sub_fa <- de$sub_fa*-1 # Higher means wants more substantive representation
    if(cor(de$surr_fa, de$surr_rep_1_1_num, use = "complete.obs") > 0) de$surr_fa <- de$surr_fa*-1 # Higher means wants (or will tolerate) more surrogate representation
    if(cor(de$desc_fa, de$desc_rep_1_1_num, use = "complete.obs") < 0) de$desc_fa <- de$desc_fa*-1 # Higher means wants more descriptive representation
    if(cor(de$just_fa, de$just_rep1_1_num, use = "complete.obs") < 0) de$just_fa <- de$just_fa*-1 # Higher means wants politicians to appeal to society as a whole
    if(cor(de$pers_fa, de$pers_rep1_4_num, use = "complete.obs") < 0) de$pers_fa <- de$pers_fa*-1 # Higher means wants more personalization
    if(cor(de$resp_fa, de$respons_rep1_2_num, use = "complete.obs") < 0) de$resp_fa <- de$resp_fa*-1 # Higher means wants more responsiveness
    
    de
    
  }
  
  
  boots_uk <- rsample::bootstraps(uk, times = n_boots)
  boots_us <- rsample::bootstraps(us, times = n_boots)
  boots_de <- rsample::bootstraps(de, times = n_boots)
  
  print("Estimating UK factors")
  
  boots_uk <- boots_uk %>%
    mutate(out = map(splits, fa_uk)) %>%
    select(out)
  
  print("Estimating US factors")
  
  boots_us <- boots_us %>%
    mutate(out = map(splits, fa_us)) %>%
    select(out)
  
  print("Estimating DE factors")
  
  boots_de <- boots_de %>%
    mutate(out = map(splits, fa_de)) %>%
    select(out)
  
  return(list(boots_uk = boots_uk, 
              boots_us = boots_us, 
              boots_de = boots_de))
  
}

# 
# for(i in 1:length(boots_uk$splits)) {
#   print(i)
#   tmp <- fa_uk(boots_uk$splits[[i]])
# }
# i<-1
# x <- analysis(boots_uk$splits[[i]])%>% 
#   # Recode batteries to numeric
#   mutate(across(all_of(all_names), ~as.numeric(factor(.x,levels = c("Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree"))), .names = "{.col}_num")) %>%
#   select(paste0(surr_rep_names,"_num"),"wgt") 
# #x$sub_rep_1_2_num <- factor(x$sub_rep_1_2_num, levels = c(1:5))
# #x[1,2] <- 1
# fa(x[,c(1,2,3,4)], nfactors = 1, fm = "ml", cor = "poly", weight = x$wgt)
# table(x[,2])
# fac
#   cor.wt(x[,-5], w = as.numeric(x$wgt))
# # Apply factor analysis function
#   mutate(sub_fa = fa_func(select(.,paste0(sub_rep_names,"_num")), wgt = wgt)
#          #surr_fa = fa_func(select(.,paste0(surr_rep_names,"_num")), wgt = wgt),
#     #     desc_fa = fa_func(select(.,paste0(desc_rep_names,"_num")), wgt = wgt),
#      #    just_fa = fa_func(select(.,paste0(just_rep_names,"_num")), wgt = wgt),
#       #   pers_fa = fa_func(select(.,paste0(pers_rep_names,"_num")), wgt = wgt),
#        #  resp_fa = fa_func(select(.,paste0(resp_rep_names,"_num")), wgt = wgt)
#   ) 
